/**
 * Copyright 2014 lerry
 * 
 * All right reserved.
 * 
 * Dec 15, 2014
 */
package com.bac.http;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.StringRequest;
import com.bac.bacplatform.BacApplication;

/**
 * @author lerry
 * 
 */
public class HeaderStringRequest extends StringRequest {

	private final Map<String, String> mParamsMap;

	/**
	 * @param method
	 * @param url
	 * @param listener
	 * @param errorListener
	 */
	public HeaderStringRequest(int method, String url,
			Map<String, String> params, Listener<String> listener,
			ErrorListener errorListener) {
		super(method, url, listener, errorListener);
		mParamsMap = params;
	}

	/**
	 * @param url
	 * @param listener
	 * @param errorListener
	 */
	public HeaderStringRequest(String url, Listener<String> listener,
			ErrorListener errorListener) {
		super(url, listener, errorListener);
		mParamsMap = new HashMap<String, String>();
	}

	@Override
	protected Response<String> parseNetworkResponse(NetworkResponse response) {
		BacApplication.getInstance().checkSessionCookie(response.headers);
		return super.parseNetworkResponse(response);
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		Map<String, String> headers = super.getHeaders();
		if (headers == null || headers.equals(Collections.emptyMap())) {
			headers = new HashMap<String, String>();
		}
		BacApplication.getInstance().addSessionCookie(headers);
		return headers;
	}

	@Override
	protected Map<String, String> getParams() throws AuthFailureError {
		return mParamsMap;
	}

//	@Override
//	public RetryPolicy getRetryPolicy() {
//		// TODO Auto-generated method stub
//		return new DefaultRetryPolicy(10 * 1000, 1, 1.0f);
//	}

	
}
